'use strict';

module.exports = app => {
  const { STRING, INTEGER, TEXT, BOOLEAN, DATE, DECIMAL, ENUM } = app.Sequelize;

  const UserSetting = app.model.define('user_setting', {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    user_id: {
      type: INTEGER,
      allowNull: false,
      unique: true,
      comment: '用户ID',
    },
    // 身体信息
    height: {
      type: INTEGER,
      comment: '身高(cm)',
    },
    weight: {
      type: DECIMAL(5, 2),
      comment: '体重(kg)',
    },
    body_type: {
      type: ENUM('pear', 'apple', 'hourglass', 'rectangle', 'inverted_triangle'),
      comment: '体型',
    },
    clothing_sizes: {
      type: TEXT,
      comment: '各类衣服尺码(JSON)',
    },
    // 预算设置
    budget_range_min: {
      type: DECIMAL(10, 2),
      comment: '预算范围最小值',
    },
    budget_range_max: {
      type: DECIMAL(10, 2),
      comment: '预算范围最大值',
    },
    // 推荐设置
    enable_weather_recommendations: {
      type: BOOLEAN,
      defaultValue: true,
      comment: '启用天气推荐',
    },
    enable_occasion_recommendations: {
      type: BOOLEAN,
      defaultValue: true,
      comment: '启用场合推荐',
    },
    enable_color_recommendations: {
      type: BOOLEAN,
      defaultValue: true,
      comment: '启用颜色推荐',
    },
    enable_social_recommendations: {
      type: BOOLEAN,
      defaultValue: false,
      comment: '启用社交推荐',
    },
    // 提醒设置
    enable_laundry_reminders: {
      type: BOOLEAN,
      defaultValue: true,
      comment: '启用洗涤提醒',
    },
    enable_seasonal_reminders: {
      type: BOOLEAN,
      defaultValue: true,
      comment: '启用换季提醒',
    },
    enable_shopping_reminders: {
      type: BOOLEAN,
      defaultValue: true,
      comment: '启用购物提醒',
    },
    laundry_reminder_days: {
      type: INTEGER,
      defaultValue: 7,
      comment: '洗涤提醒天数',
    },
    // 隐私设置
    profile_visibility: {
      type: ENUM('public', 'friends', 'private'),
      defaultValue: 'friends',
      comment: '个人资料可见性',
    },
    outfit_sharing_default: {
      type: ENUM('public', 'friends', 'private'),
      defaultValue: 'friends',
      comment: '搭配分享默认设置',
    },
    allow_friend_requests: {
      type: BOOLEAN,
      defaultValue: true,
      comment: '允许好友请求',
    },
    // 通知设置
    email_notifications: {
      type: BOOLEAN,
      defaultValue: true,
      comment: '邮件通知',
    },
    push_notifications: {
      type: BOOLEAN,
      defaultValue: true,
      comment: '推送通知',
    },
    notification_time: {
      type: STRING(10),
      defaultValue: '09:00',
      comment: '通知时间',
    },
    // 界面设置
    preferred_language: {
      type: STRING(10),
      defaultValue: 'zh-CN',
      comment: '偏好语言',
    },
    timezone: {
      type: STRING(50),
      defaultValue: 'Asia/Shanghai',
      comment: '时区',
    },
    theme: {
      type: ENUM('light', 'dark', 'auto'),
      defaultValue: 'auto',
      comment: '主题',
    },
    // 功能设置
    auto_categorize: {
      type: BOOLEAN,
      defaultValue: true,
      comment: '自动分类',
    },
    auto_tag: {
      type: BOOLEAN,
      defaultValue: true,
      comment: '自动标签',
    },
    auto_color_detection: {
      type: BOOLEAN,
      defaultValue: true,
      comment: '自动颜色识别',
    },
    created_at: {
      type: DATE,
      allowNull: false,
      defaultValue: app.Sequelize.NOW,
    },
    updated_at: {
      type: DATE,
      allowNull: false,
      defaultValue: app.Sequelize.NOW,
    },
  }, {
    tableName: 'user_settings',
    timestamps: true,
    paranoid: false,  // 禁用软删除
    underscored: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    comment: '用户设置表',
  });

  // 关联关系
  UserSetting.associate = function() {
    // 属于用户
    app.model.UserSetting.belongsTo(app.model.User, {
      foreignKey: 'user_id',
      as: 'user',
    });
  };

  return UserSetting;
};
